package itdep.jsf.internal;

import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.component.StateHolder;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

/**
 * todo: delete this
 *
 * @author Ivan Khalopik
 * @version $Revision: 21 $ $Date: 2010-06-19 06:52:13 +0000 (Sat, 19 Jun 2010) $
 */
@Deprecated
public class DelegateConverter implements Converter, StateHolder {
	private ValueExpression converter;

	public DelegateConverter() {
	}

	public DelegateConverter(final ValueExpression converter) {
		this.converter = converter;
	}

	public Object getAsObject(final FacesContext context, final UIComponent component, final String value) throws ConverterException {
		return getConverter(context).getAsObject(context, component, value);
	}

	public String getAsString(final FacesContext context, final UIComponent component, final Object value) throws ConverterException {
		return getConverter(context).getAsString(context, component, value);
	}

	public Object saveState(final FacesContext context) {
		return converter;
	}

	public void restoreState(final FacesContext context, final Object state) {
		converter = (ValueExpression) state;
	}

	public boolean isTransient() {
		return false;
	}

	public void setTransient(final boolean newTransientValue) {
		//do nothing
	}

	private Converter getConverter(final FacesContext context) {
		final ELContext elContext = context.getELContext();
		if (converter != null) {
			return (Converter) converter.getValue(elContext);
		}
		throw new IllegalStateException("no converter converter specified");
	}
}
